package com.zggk.iroad.land;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpermission.PermissionConstants;
import com.lxj.xpermission.XPermission;
import com.zggk.iroad.R;
import com.zggk.iroad.api.MyAppliCation;
import com.zggk.iroad.main.MainActivity;
import com.zggk.iroad.mvp.MVPBaseActivity;
import com.zggk.iroad.util.MyUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class LandActivity extends MVPBaseActivity<LandContract.View, LandPresenter> implements LandContract.View {


    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_yanzhengma)
    EditText etYanzhengma;
    @Bind(R.id.login_bt)
    TextView loginBt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

    }

    @Override
    protected int getLayout() {
        return R.layout.act_land;
    }

    @Override
    public void onRequestError(String msg) {
        showToast(msg, Toast.LENGTH_SHORT, Gravity.BOTTOM);
        loginBt.setClickable(true);
    }

    @Override
    public void getLand(LandInfo landInfo) {
        loginBt.setClickable(true);
        MyAppliCation.spUtils.put("UserName", etPhone.getText().toString().trim());//用户名
        MyAppliCation.spUtils.put("GydwId", landInfo.getDQGYDW());//管养单位id
        MyAppliCation.spUtils.put("GydwMc", landInfo.getDQGYDWMC());//管养单位名称
        MyAppliCation.spUtils.put("OrgName", landInfo.getDLR());//登陆人
        MyAppliCation.spUtils.put("ResouRce", landInfo.getRESOURCE());//权限
        mPresenter.getName();
    }

    @Override
    public void getNames(String nameBeans) {
        MyAppliCation.spUtils.put("NAMES", nameBeans);
        showToast("登陆成功", Toast.LENGTH_SHORT, Gravity.BOTTOM);
        Intent intent = new Intent(LandActivity.this, MainActivity.class);
        intent.putExtra("isDown", true);
        startActivity(intent);
        finish();
    }

    @OnClick({R.id.login_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_bt:
                hideSoftInputFromWindow(LandActivity.this);
                loginBt.setClickable(false);
                XPermission xPermission;
                xPermission = XPermission.create(LandActivity.this, PermissionConstants.STORAGE//存储控件
                        , PermissionConstants.LOCATION//位置信息
                        , PermissionConstants.CAMERA//相机
                        , PermissionConstants.MICROPHONE//录制音频
                );
                xPermission.callback(new XPermission.SimpleCallback() {
                    @Override
                    public void onGranted() {
                        String username = etPhone.getText().toString().trim();
                        String password = etYanzhengma.getText().toString().trim();
                        if (MyUtils.isNull(username) && MyUtils.isNull(password)) {
                            ToastUtils.showShort("账户或密码不能为空");
                            return;
                        } else {
                            mPresenter.Land(username, password,LandActivity.this);
                        }
                    }

                    @Override
                    public void onDenied() {
                        ToastUtils.showShort("没有权限，无法使用该功能");
                    }
                }).request();
                break;
        }
    }
    /**
     * 隐藏输入面板
     *
     * @param activity
     * @return true 成功隐藏面板，false 没有隐藏面板或者没有面板可以隐藏
     */
    public static boolean hideSoftInputFromWindow(Activity activity) {
        if (activity != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                return imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
            }
        }
        return false;
    }
}